package com.duke.shaking.widget;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.duke.shaking.Constants;
import com.duke.shaking.R;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.duke.shaking.register.Register2Activity;

public class TextEditActivity extends TitleActivity {

	private int tipId;
	private int titleId;
	private String original;
	private boolean multiLine;
	private int editCode;
	
	private TextView txt_tip;
	private EditText edit_input;
	private String input;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		tipId = 0;
		titleId = 0;
		original = null;
		Intent i = getIntent();
		tipId = i.getIntExtra("tip", 0);
		titleId = i.getIntExtra("title", 0);
		original = i.getStringExtra("original");
		multiLine = i.getBooleanExtra("multiLine", false);
		editCode = i.getIntExtra("editCode", -1);
		
		super.onCreate(savedInstanceState);
		txt_tip = (TextView)findViewById(R.id.txt_tip);
		edit_input = (EditText)findViewById(R.id.edit_input);
		if (txt_tip != null && tipId != 0) {
			txt_tip.setText(tipId);
		}
		if (edit_input != null) {
			if (multiLine) {
				edit_input.setSingleLine(false);
				edit_input.setLines(3);
			}
			
			if (original != null) {
				edit_input.setText(original);
			}
		}
	}
	
	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_text_edit;
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				TITLE_BTN_EXIST,
				R.string.save,
				titleId);
	}
	
	public void onLeftBtn(View v) {
		Intent intent = new Intent();  
        setResult(RESULT_CANCELED, intent); 
        finish(); 
	}
	
	public void onRightBtn(View v) {
		if (edit_input == null) {
			return;
		}
		
		input = edit_input.getText().toString();
		if (input == null) {
			input = "";
		}
		
		Map<String, String> params = new HashMap<String, String>();
		if(editCode != -1) {
			switch (editCode) {
			case Constants.INTENT_CODE_EDIT_NICKNAME: {
					if (input.equals("")) {
						toast(R.string.err_nickname_cannot_be_empty);
						return;
					}
					params.put(NetUtils.NICKNAME, input);
				}
				break;
				
			case Constants.INTENT_CODE_EDIT_SCHOOL:
				params.put(NetUtils.SCHOOL, input);
				break;
				
			case Constants.INTENT_CODE_EDIT_CAREER:
				params.put(NetUtils.JOB, input);
				break;
			case Constants.INTENT_CODE_EDIT_SIGNATURE:
				params.put(NetUtils.REMARKS, input);
			default:
				break;
			}
		}
		
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				// TODO Auto-generated method stub
				super.onResultSuccess(response);
				
				afterSaveSuccess();
			}

			@Override
			public void onResultFailure(String result) {
				// TODO Auto-generated method stub
				super.onResultFailure(result);
				
				toast(R.string.err_save);
			}
        };
        
        params.put(NetUtils.TOKEN, me.getAuthToken());
        NetUtils.updateProfile(this, responseHandler, params, null);
	}
	
	private void afterSaveSuccess() {
		Intent intent = new Intent(); 
        intent.putExtra("input", input); 
        setResult(RESULT_OK, intent); 
        finish(); 
	}

}
